﻿using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Microsoft.Extensions.Caching.Memory;
using Nuctech.DIS.Publisher;
using System.Data.SQLite;

namespace GrpcService2.Services
{
    public class DisService : Nuctech.DIS.Publisher.StatusPulishServer.StatusPulishServerBase
    {
        private readonly IMemoryCache _memoryCahe;
        public DisService(IMemoryCache memoryCahe) 
        {

            this._memoryCahe = memoryCahe;
        }

        public override Task<DISStatusRes> QueryDISStatus(Empty request, ServerCallContext context)
        {
            var disStatusRes = new DISStatusRes();
            var Infos = new List<StatusInfo>
            {
                new StatusInfo()
                {
                    Code =Nuctech.DIS.Publisher.StatusCode.StateDisStateBackup,
                    Details="Dis status ok",
                    Type=0
                },
                     new StatusInfo()
                {
                    Code =Nuctech.DIS.Publisher.StatusCode.StateOk,
                    Details="NAS status ok1",
                    Type=1
                },
                      new StatusInfo()
                {
                    Code =Nuctech.DIS.Publisher.StatusCode.StateOk,
                    Details="Db status ok1",
                    Type=2
                }
            };

            disStatusRes.Infos.AddRange(Infos);
            return Task.FromResult(disStatusRes);

            //return base.QueryDISStatus(request, context);
        }

        public override Task<DISQueueInfoRes> QueryQueueInfos(Empty request, ServerCallContext context)
        {
            var res = new DISQueueInfoRes();

            var list = new List<DISQueueInfo>
            {
                new DISQueueInfo()
                {
                    Type=1,
                    UsedLen=30,
                    TotalLen=100
                },
                new DISQueueInfo()
                {
                    Type=2,
                    UsedLen=60,
                    TotalLen=100
                },
                new DISQueueInfo()
                {
                    Type=3,
                    UsedLen=90,
                    TotalLen=100
                }
            };
            res.Queueinfo.AddRange(list);
            return Task.FromResult(res);
            //return base.QueryQueueInfos(request, context);
        }

        public override Task<DISClientListRes> QueryClientList(Empty request, ServerCallContext context)
        {
            var res= new DISClientListRes();

            //var list = Enumerable.Range(500000, 1500000)
            var list = Enumerable.Range(1, 10)
                .Select(s => new DISClientInfo
                {
                    Type = s,
                    Clientid = "00-201-116719977132-RccServer-" + s,
                    Ip = "192.169.0." + s,
                    Sessionid = Guid.NewGuid().ToString(),
                    State = 0,
                });

            res.Clientinfo.AddRange(list);

            return Task.FromResult(res);

            //return base.QueryClientList(request, context);
        }

        public override Task<Result> SetHisRecordTable(HisRecordTableSetRequest request, ServerCallContext context)
        {
            var res = new Result
            {
                Details = "备机合并成功",
                ErrorCode = errorcodes.Ok,
            };

            res.Errors.AddRange(new List<errorcodes>
            {
                errorcodes.Ok ,
                errorcodes.Failed,
            });
            return Task.FromResult(res);
            //  return base.SetHisRecordTable(request, context);
        }

        //public override Task<Result> SetHisRecordTableStream(IAsyncStreamReader<HisRecordTableSetRequest> request, ServerCallContext context)
        //{
        //    var res = new Result
        //    {
        //        Details = "备机合并成功",
        //        ErrorCode = errorcodes.Ok,
        //    };

        //    res.Errors.AddRange(new List<errorcodes>
        //    {
        //        errorcodes.Ok ,
        //        errorcodes.Failed,
        //    });
        //    return Task.FromResult(res);
        //    //  return base.SetHisRecordTable(request, context);
        //}

        

        public override Task<HisTableResult> QueryHisRecordTable(Empty request, ServerCallContext context)
        {
            var res = new HisTableResult();
            

            //var list = Enumerable.Range(1, 1000003)
            //    .Select(s => new HisTableRecord
            //    {
            //        Deviceid = "ips3d" + s,
            //        Prn = "Nuctech20241211S000" + s,
            //        Finished=0,
            //        Timestamp= (ulong)(1733906666000),
            //    });

            var list = GetHisTableRecordsBySqlite();

            res.Records.AddRange(list);

            _memoryCahe.Set("test", res);

          var ss=  _memoryCahe.Get<HisTableResult>("test");

            return Task.FromResult<HisTableResult>(res);
            //return base.QueryHisRecordTable(request, context);
        }



        public List<HisTableRecord> GetHisTableRecordsBySqlite()
        {
            List<HisTableRecord> records = new List<HisTableRecord>();

            string connectionString = "Data Source=D:\\Db\\DIS.db;Version=3;";

            using (SQLiteConnection sQLite = new SQLiteConnection(connectionString))
            {
                sQLite.Open();

                string sql = "SELECT deviceid,prn,timestamp FROM  bag_his_record_tbl ";

                SQLiteCommand command=new SQLiteCommand(sql,sQLite);

                SQLiteDataReader sQLiteDataReader = command.ExecuteReader();
                while (sQLiteDataReader.Read())
                {
                    var Deviceid = sQLiteDataReader.GetString(0);
                    var Prn = sQLiteDataReader.GetString(1);
                    //var Finished = sQLiteDataReader.GetInt32(2);
                    var Timestamp = (ulong)sQLiteDataReader.GetInt64(2);

                    HisTableRecord hisTableRecord = new HisTableRecord()
                    {
                        Deviceid = sQLiteDataReader.GetString(0),
                        Prn = sQLiteDataReader.GetString(1),
                        Finished = 0,
                        Timestamp = (ulong)sQLiteDataReader.GetInt64(2)
                    };

                    records.Add(hisTableRecord);
                }

                return records;
            }

                
        }
    }
}
